/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.news.service.impl;

import com.ktjiaoyu.news.dao.NewsMemberDao;
import com.ktjiaoyu.news.dao.impl.NewsMemberDaoImpl;
import com.ktjiaoyu.news.entity.NewsMember;
import com.ktjiaoyu.news.mapper.NewsMemberMapper;
import com.ktjiaoyu.news.service.NewsMemberService;
import com.ktjiaoyu.news.util.MyBatisUtil;
import com.ktjiaoyu.news.utils.Page;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 系统会员。游客只能浏览新闻，会员可以发布评论，收藏新闻 Service接口实现
 * @author 科泰集团教研团队
 * @createTime 2024-4-9 17:36:33
 * @lastModify 2024-4-9 17:36:33
 * @version 1.0.0
 */
public class NewsMemberServiceImpl implements NewsMemberService {


    @Override
    public Page<NewsMember> findPage(NewsMember entity, int pageNo, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        Page<NewsMember> page = new Page<>();
        // 获得总数量
        int count = sqlSession.getMapper(NewsMemberMapper.class).count(entity);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 设置总数量，计算总页数
        page.setTotalCount(count);
        List<NewsMember> list = sqlSession.getMapper(NewsMemberMapper.class).getPage(entity,(pageNo-1)*pageSize,pageSize);
        page.setData(list);
        MyBatisUtil.closeSqlSession(sqlSession);
        return page;
    }

    @Override
    public List<NewsMember> findAll() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<NewsMember> list = sqlSession.getMapper(NewsMemberMapper.class).getAll();
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public NewsMember findById(Long id) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        NewsMember byId = sqlSession.getMapper(NewsMemberMapper.class).getById(id);
        MyBatisUtil.closeSqlSession(sqlSession);
        return byId;
    }

    @Override
    public boolean saveOrUpdate(NewsMember entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean result = false;
        if(entity.getId() != null){
            // 如果有ID就是修改
            result = sqlSession.getMapper(NewsMemberMapper.class).update(entity)> 0;
        }
        // 没有id就是新增
           result=sqlSession.getMapper(NewsMemberMapper.class).insert(entity)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    @Override
    public boolean importBatch(NewsMember... entities) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean result = false;
        result= sqlSession.getMapper(NewsMemberMapper.class).insertBatch(entities)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }


    @Override
    public boolean removeById(Long id) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean result = false;
        result = sqlSession.getMapper(NewsMemberMapper.class).deleteById(id) > 0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);

        return result;
    }

    @Override
    public boolean removeByEntity(NewsMember entity) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean result = false;
        result=sqlSession.getMapper(NewsMemberMapper.class).deleteByEntity(entity)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    @Override
    public boolean removeBatch(Long... ids) {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        boolean result = false;
        result=sqlSession.getMapper(NewsMemberMapper.class).deleteBatch(ids)>0;
        sqlSession.commit();
        MyBatisUtil.closeSqlSession(sqlSession);
        return result;
    }
}
